


package com.starmark.sys.org.atomic.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starmark.core.framework.base.AbstractController;
import com.starmark.core.framework.base.IdNameModel;
import com.starmark.core.framework.util.BeanMapper;
import com.starmark.core.framework.vo.PageQueryVo;
import com.starmark.sys.org.atomic.entity.SysOrgPost;
import com.starmark.sys.org.atomic.service.ISysOrgPostService;
import com.starmark.sys.org.atomic.vo.SysOrgPostPageVo;
import com.starmark.sys.org.atomic.vo.SysOrgPostVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 岗位 前端控制器
 * </p>
 *
 * @author starmark
 * @since 2020-08-06
 */
@RestController
@Api(value = "岗位管理", description = "岗位管理")
@RequestMapping("sys/org/post")
public class SysOrgPostController extends AbstractController {
    @Autowired
    ISysOrgPostService sysOrgPostService;

    @ApiOperation(value = "查询岗位列表")
    @PostMapping(value = "/query")
    public Page<SysOrgPostPageVo> query(@RequestBody PageQueryVo pageQueryVo) {

        return BeanMapper.mapPage(sysOrgPostService.queryPage(pageQueryVo), SysOrgPostPageVo.class);
    }


    @ApiOperation(value = "新增岗位")
    @PostMapping
    public SysOrgPostVo add(@RequestBody SysOrgPostVo sysOrgPostVo) {

        SysOrgPost sysOrgPost = BeanMapper.map(sysOrgPostVo, SysOrgPost.class);
        sysOrgPostService.save(sysOrgPost);
        return BeanMapper.map(sysOrgPost, SysOrgPostVo.class);
    }

    @ApiOperation(value = "岗位详情")
    @GetMapping(value = "/{id}")
    public SysOrgPostVo get(@PathVariable("id") String id) {
        SysOrgPost sysOrgPost = sysOrgPostService.getById(id);
        return BeanMapper.map(sysOrgPost, SysOrgPostVo.class);
    }

    @PutMapping
    @ApiOperation(value = "修改岗位")
    public SysOrgPostVo update(@RequestBody SysOrgPostVo sysOrgPostVo) {
        SysOrgPost sysOrgPost = BeanMapper.map(sysOrgPostVo, SysOrgPost.class);
        sysOrgPostService.updateById(sysOrgPost);
        return BeanMapper.map(sysOrgPost, SysOrgPostVo.class);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "删除岗位")
    public void delete(@PathVariable("id") String id) {

        sysOrgPostService.removeById(id);
    }

    @ApiOperation(value = "岗位下拉列表")
    @GetMapping(value = "/postList")
    public List<IdNameModel> postList() {
        List<SysOrgPost> sysOrgPostList = sysOrgPostService.list();
        return sysOrgPostList.stream().map(sysOrgPost -> new IdNameModel(sysOrgPost.getId(), sysOrgPost.getPostName())).collect(Collectors.toList());
    }
}
